package com.flyemu.share.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.flyemu.share.api.Fenxi;
import com.flyemu.share.api.FenxiLogin;
import com.flyemu.share.entity.QRelationAccount;
import com.flyemu.share.entity.RelationAccount;
import com.flyemu.share.repository.RelationAccountRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class RelationAccountService extends AbsService {
    private final RelationAccountRepository accountRepository;
    private final FenxiLogin fenxiLogin;
    private final QRelationAccount qRelationAccount = QRelationAccount.relationAccount;

    @Transactional
    public void save(RelationAccount relationAccount) {
        String cookie = fenxiLogin.getCookie(relationAccount);
        relationAccount.setCookie(cookie);
        if (relationAccount.getId() != null) {
            RelationAccount original = accountRepository.getById(relationAccount.getId());
            BeanUtil.copyProperties(relationAccount, original, CopyOptions.create().ignoreNullValue());
            accountRepository.save(original);
        }

        accountRepository.save(relationAccount);
    }

    @Transactional
    public RelationAccount loadById(Long id, Long merchantId, Long organizationId) {
        return  bqf.selectFrom(qRelationAccount)
                .where(qRelationAccount.id.eq(id).and(qRelationAccount.merchantId.eq(merchantId)).and(qRelationAccount.organizationId.eq(organizationId))).fetchFirst();
    }

    @Transactional
    public RelationAccount load(Long merchantId, Long organizationId) {
        RelationAccount relationAccount = bqf.selectFrom(qRelationAccount)
                .where(qRelationAccount.merchantId.eq(merchantId).and(qRelationAccount.organizationId.eq(organizationId))).fetchFirst();
        if (StrUtil.isEmpty(relationAccount.getCookie())) {
            String cookie = fenxiLogin.getCookie(relationAccount);
            jqf.update(qRelationAccount)
                    .set(qRelationAccount.cookie, cookie)
                    .where(qRelationAccount.id.eq(relationAccount.getId())).execute();
            relationAccount.setCookie(cookie);
        }
        return relationAccount;
    }

    @Transactional
    public RelationAccount upCookie(Long merchantId, Long organizationId) {
        RelationAccount relationAccount = bqf.selectFrom(qRelationAccount)
                .where(qRelationAccount.merchantId.eq(merchantId).and(qRelationAccount.organizationId.eq(organizationId))).fetchFirst();
        String cookie = fenxiLogin.getCookie(relationAccount);
        jqf.update(qRelationAccount)
                .set(qRelationAccount.cookie, cookie)
                .where(qRelationAccount.id.eq(relationAccount.getId())).execute();
        relationAccount.setCookie(cookie);
        return relationAccount;
    }
}
